package com.lusheng.config;


import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.List;

@AutoConfiguration  // 表示当前是一个自动配置类
public class MyBatisAutoConfig {
	// sqlsessionFactoryBean
	@Bean
	public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		return sqlSessionFactoryBean;
	}
	// MapperScannerConfigure
	@Bean
	public MapperScannerConfigurer mapperscannerConfigurer(BeanFactory beanFactory){
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		// 扫描的包：启动类所在的包及其子包
		List<String> packages = AutoConfigurationPackages.get(beanFactory);
		String s = packages.get(0);
		mapperScannerConfigurer.setBasePackage(s);

		// 扫描的注解
		mapperScannerConfigurer.setAnnotationClass(Mapper.class);
		return mapperScannerConfigurer;
	}
}
